package com.cy.pj.common.util;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 定义分页操作工具类,在此类方法内部启动分页查询
 */
public class PageUtil {
    public static <T>Page<T> startPage(){
        //获取请求对象
        ServletRequestAttributes sra
                =(ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        HttpServletRequest request=sra.getRequest();
        //获取请求中的分页参数对象
        String pageCurrentStr=request.getParameter("pageCurrent");
        String pageSizeStr=request.getParameter("pageSize");

        int pageCurrent=StringUtil
                .isEmpty(pageCurrentStr)?1:Integer
                .parseInt(pageCurrentStr);

        int pageSize=StringUtil.
                isEmpty(pageSizeStr)?10:Integer
                .parseInt(pageSizeStr);
        //启动分页查询
        return PageHelper.startPage(pageCurrent,pageSize);
    }
}
